package org.demo.system.service.impl;

import org.demo.base.auth.domain.SysUser;
import org.demo.base.constant.RoleKeys;
import org.demo.system.mapper.UserMapper;
import org.demo.system.service.RoleService;
import org.demo.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author tomdog
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleService roleService;

    /**
     * 这里需要对用户进行注册的验证
     * 有错就抛出RuntimeException
     * @param user
     */
    @Override
    public void checkRegister(SysUser user) {

    }

    /**
     * 新增一个用户
     * @param user
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public SysUser insert(SysUser user) {
        // 先保存用户
        userMapper.insert(user);
        // 然后给这个用户添加角色
        // roleService.addRoleForUser(user.getId(), RoleKeys.NORMAL);
        return user;
    }

    /**
     * 根据手机号查询用户
     * @param mobile
     * @return
     */
    @Override
    public SysUser getUserByMobile(String mobile) {
        return userMapper.getUserByMobile(mobile);
    }

    /**
     * 修改用户头像
     * @param avatar
     * @param userId
     */
    @Override
    public void updateAvatar(String avatar, Long userId) {
        userMapper.updateAvatar(avatar, userId);
    }

    @Override
    public void updateUser(SysUser user) {
        userMapper.updateUser(user);
    }

    @Override
    public void addUsers(List<SysUser> users) {
        userMapper.addUsers(users);
    }
}
